//-----------------------------------------------------------------------
// <copyright file="a_ArticleDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;
using System.Collections.Generic;

namespace ThoiTrang.Data
{
    public abstract class a_ArticleDAOBase
    {
        #region Common methods
        public virtual a_Article Createa_ArticleFromReader(IDataReader reader)
        {
            a_Article item = new a_Article();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) item.ID = (int)reader["ID"];
                if (!reader.IsDBNull(reader.GetOrdinal("UTitle"))) item.UTitle = (string)reader["UTitle"];
                if (!reader.IsDBNull(reader.GetOrdinal("KTitle"))) item.KTitle = (string)reader["KTitle"];
                if (!reader.IsDBNull(reader.GetOrdinal("UAuthor"))) item.UAuthor = (string)reader["UAuthor"];
                if (!reader.IsDBNull(reader.GetOrdinal("UBody"))) item.UBody = (string)reader["UBody"];
                if (!reader.IsDBNull(reader.GetOrdinal("KBody"))) item.KBody = (string)reader["KBody"];
                if (!reader.IsDBNull(reader.GetOrdinal("MainCatID"))) item.MainCatID = (int)reader["MainCatID"];
                if (!reader.IsDBNull(reader.GetOrdinal("SubCatID"))) item.SubCatID = (int)reader["SubCatID"];
                if (!reader.IsDBNull(reader.GetOrdinal("CountryID"))) item.CountryID = (int)reader["CountryID"];
                if (!reader.IsDBNull(reader.GetOrdinal("SeasonID"))) item.SeasonID = (int)reader["SeasonID"];
                if (!reader.IsDBNull(reader.GetOrdinal("FYear"))) item.FYear = (int)reader["FYear"];
                if (!reader.IsDBNull(reader.GetOrdinal("PostDate"))) item.PostDate = (DateTime)reader["PostDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("PostBy_UserID"))) item.PostBy_UserID = (int)reader["PostBy_UserID"];
                if (!reader.IsDBNull(reader.GetOrdinal("PublishDate"))) item.PublishDate = (DateTime)reader["PublishDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("PublishStatus"))) item.PublishStatus = (bool)reader["PublishStatus"];
                if (!reader.IsDBNull(reader.GetOrdinal("StatPopularity"))) item.StatPopularity = (int)reader["StatPopularity"];
                if (!reader.IsDBNull(reader.GetOrdinal("StatRating"))) item.StatRating = (byte)reader["StatRating"];
                if (!reader.IsDBNull(reader.GetOrdinal("Permission"))) item.Permission = (bool)reader["Permission"];
                if (!reader.IsDBNull(reader.GetOrdinal("IsFeatured"))) item.IsFeatured = (bool)reader["IsFeatured"];
                if (!reader.IsDBNull(reader.GetOrdinal("FeaturedType"))) item.FeaturedType = (byte)reader["FeaturedType"];
                if (!reader.IsDBNull(reader.GetOrdinal("LayoutFormat"))) item.LayoutFormat = (byte)reader["LayoutFormat"];
                if (!reader.IsDBNull(reader.GetOrdinal("UniqueKeywords"))) item.UniqueKeywords = (string)reader["UniqueKeywords"];
                if (!reader.IsDBNull(reader.GetOrdinal("IsEdited"))) item.IsEdited = (bool)reader["IsEdited"];
                if (!reader.IsDBNull(reader.GetOrdinal("Brand"))) item.Brand = (string)reader["Brand"];
                if (!reader.IsDBNull(reader.GetOrdinal("GroupID"))) item.GroupID = (int)reader["GroupID"];
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return item;
        }
        #endregion

        #region Createa_Article methods

        public virtual void Createa_Article(a_Article a_Article)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Articles_ins");

                database.AddInParameter(dbCommand, "@UTitle", DbType.String, a_Article.UTitle);
                database.AddInParameter(dbCommand, "@KTitle", DbType.AnsiString, a_Article.KTitle);
                database.AddInParameter(dbCommand, "@UAuthor", DbType.String, a_Article.UAuthor);
                database.AddInParameter(dbCommand, "@UBody", DbType.String, a_Article.UBody);
                database.AddInParameter(dbCommand, "@KBody", DbType.AnsiString, a_Article.KBody);
                database.AddInParameter(dbCommand, "@MainCatID", DbType.Int32, a_Article.MainCatID);
                database.AddInParameter(dbCommand, "@SubCatID", DbType.Int32, a_Article.SubCatID);
                database.AddInParameter(dbCommand, "@CountryID", DbType.Int32, a_Article.CountryID);
                database.AddInParameter(dbCommand, "@SeasonID", DbType.Int32, a_Article.SeasonID);
                database.AddInParameter(dbCommand, "@FYear", DbType.Int32, a_Article.FYear);
                database.AddInParameter(dbCommand, "@PostDate", DbType.DateTime, a_Article.PostDate);
                database.AddInParameter(dbCommand, "@PostBy_UserID", DbType.Int32, a_Article.PostBy_UserID);
                database.AddInParameter(dbCommand, "@PublishDate", DbType.DateTime, a_Article.PublishDate);
                database.AddInParameter(dbCommand, "@PublishStatus", DbType.Boolean, a_Article.PublishStatus);
                database.AddInParameter(dbCommand, "@StatPopularity", DbType.Int32, a_Article.StatPopularity);
                database.AddInParameter(dbCommand, "@StatRating", DbType.Byte, a_Article.StatRating);
                database.AddInParameter(dbCommand, "@Permission", DbType.Boolean, a_Article.Permission);
                database.AddInParameter(dbCommand, "@IsFeatured", DbType.Boolean, a_Article.IsFeatured);
                database.AddInParameter(dbCommand, "@FeaturedType", DbType.Byte, a_Article.FeaturedType);
                database.AddInParameter(dbCommand, "@LayoutFormat", DbType.Byte, a_Article.LayoutFormat);
                database.AddInParameter(dbCommand, "@UniqueKeywords", DbType.AnsiString, a_Article.UniqueKeywords);
                database.AddInParameter(dbCommand, "@IsEdited", DbType.Boolean, a_Article.IsEdited);
                database.AddInParameter(dbCommand, "@Brand", DbType.String, a_Article.Brand);
                database.AddInParameter(dbCommand, "@GroupID", DbType.Int32, a_Article.GroupID);
                database.AddOutParameter(dbCommand, "@ID", DbType.Int32, 0);

                database.ExecuteNonQuery(dbCommand);
                a_Article.ID = (int)database.GetParameterValue(dbCommand, "@ID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_Article methods

        public virtual void Updatea_Article(a_Article a_Article)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Articles_upd");

                database.AddInParameter(dbCommand, "@ID", DbType.Int32, a_Article.ID);
                database.AddInParameter(dbCommand, "@UTitle", DbType.String, a_Article.UTitle);
                database.AddInParameter(dbCommand, "@KTitle", DbType.AnsiString, a_Article.KTitle);
                database.AddInParameter(dbCommand, "@UAuthor", DbType.String, a_Article.UAuthor);
                database.AddInParameter(dbCommand, "@UBody", DbType.String, a_Article.UBody);
                database.AddInParameter(dbCommand, "@KBody", DbType.AnsiString, a_Article.KBody);
                database.AddInParameter(dbCommand, "@MainCatID", DbType.Int32, a_Article.MainCatID);
                database.AddInParameter(dbCommand, "@SubCatID", DbType.Int32, a_Article.SubCatID);
                database.AddInParameter(dbCommand, "@CountryID", DbType.Int32, a_Article.CountryID);
                database.AddInParameter(dbCommand, "@SeasonID", DbType.Int32, a_Article.SeasonID);
                database.AddInParameter(dbCommand, "@FYear", DbType.Int32, a_Article.FYear);
                database.AddInParameter(dbCommand, "@PostDate", DbType.DateTime, a_Article.PostDate);
                database.AddInParameter(dbCommand, "@PostBy_UserID", DbType.Int32, a_Article.PostBy_UserID);
                database.AddInParameter(dbCommand, "@PublishDate", DbType.DateTime, a_Article.PublishDate);
                database.AddInParameter(dbCommand, "@PublishStatus", DbType.Boolean, a_Article.PublishStatus);
                database.AddInParameter(dbCommand, "@StatPopularity", DbType.Int32, a_Article.StatPopularity);
                database.AddInParameter(dbCommand, "@StatRating", DbType.Byte, a_Article.StatRating);
                database.AddInParameter(dbCommand, "@Permission", DbType.Boolean, a_Article.Permission);
                database.AddInParameter(dbCommand, "@IsFeatured", DbType.Boolean, a_Article.IsFeatured);
                database.AddInParameter(dbCommand, "@FeaturedType", DbType.Byte, a_Article.FeaturedType);
                database.AddInParameter(dbCommand, "@LayoutFormat", DbType.Byte, a_Article.LayoutFormat);
                database.AddInParameter(dbCommand, "@UniqueKeywords", DbType.AnsiString, a_Article.UniqueKeywords);
                database.AddInParameter(dbCommand, "@IsEdited", DbType.Boolean, a_Article.IsEdited);
                database.AddInParameter(dbCommand, "@Brand", DbType.String, a_Article.Brand);
                database.AddInParameter(dbCommand, "@GroupID", DbType.Int32, a_Article.GroupID);

                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Deletea_Article methods
        public virtual void Deletea_Article(int id)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Articles_del");

                database.AddInParameter(dbCommand, "@ID", DbType.Int32, id);

                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Geta_Article methods

        public virtual a_Article Geta_Article(int id)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Articles_sel");

                database.AddInParameter(dbCommand, "@ID", DbType.Int32, id);

                a_Article a_Article = null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_Article = Createa_ArticleFromReader(reader);
                        reader.Close();
                    }
                }
                return a_Article;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Geta_ArticleList methods
        public virtual a_ArticleCollection Geta_ArticleList(a_ArticleColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Articles_selList");

                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);

                a_ArticleCollection a_ArticleCollection = new a_ArticleCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Article a_Article = Createa_ArticleFromReader(reader);
                        a_ArticleCollection.Add(a_Article);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_ArticleCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public virtual a_ArticleCollection Geta_ArticleList(a_ArticleColumns orderBy, OrderDirection orderDirection)
        {
            int totalRecords = 0;
            return Geta_ArticleList(orderBy, orderDirection, 0, 0, out totalRecords);
        }

        #endregion

        #region Geta_ArticleListYear methods
        public virtual int[] Geta_ArticleListYear(int scid)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Articles_selListYear");
                database.AddInParameter(dbCommand, "@scid", DbType.Int32, scid);

                List<int> years = new List<int>();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        years.Add(reader.GetInt32(0));
                    }
                    reader.Close();
                }
                return years.ToArray();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion


        /******************************************************************************
        **	    Nguyen Minh Duc
        *******************************************************************************/
        #region Geta_ArticleListNewr methods
        public virtual a_ArticleCollection Geta_ArticleListNewer(int SubCat, int IdItem, a_ArticleColumns orderBy, OrderDirection orderDirection, int page, int pageSize)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Articles_selListNewer");

                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddInParameter(dbCommand, "@SubCat", DbType.Int32, SubCat);
                database.AddInParameter(dbCommand, "@IdItem", DbType.Int32, IdItem);

                a_ArticleCollection a_ArticleCollection = new a_ArticleCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Article a_Article = Createa_ArticleFromReader(reader);
                        a_ArticleCollection.Add(a_Article);
                    }
                    reader.Close();
                }                
                return a_ArticleCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region a_Articles_selListLaster methods
        public virtual a_ArticleCollection a_Articles_selListLaster(int SubCat, int IdItem, a_ArticleColumns orderBy, OrderDirection orderDirection, int page, int pageSize)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Articles_selListLaster");

                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddInParameter(dbCommand, "@SubCat", DbType.Int32, SubCat);
                database.AddInParameter(dbCommand, "@IdItem", DbType.Int32, IdItem);

                a_ArticleCollection a_ArticleCollection = new a_ArticleCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Article a_Article = Createa_ArticleFromReader(reader);
                        a_ArticleCollection.Add(a_Article);
                    }
                    reader.Close();
                }
                return a_ArticleCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion
        /******************************************************************************
        **	    End Nguyen Minh Duc
        *******************************************************************************/


        public virtual a_ArticleCollection Geta_ArticleListBySubIdAndMainId(int mainId, int subId, a_ArticleColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Articles_selListBySubIdAndMainId");

                database.AddInParameter(dbCommand, "@MainCatID", DbType.Int32, mainId);
                database.AddInParameter(dbCommand, "@SubCatID", DbType.Int32, subId);
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);

                a_ArticleCollection a_ArticleCollection = new a_ArticleCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Article a_Article = Createa_ArticleFromReader(reader);
                        a_ArticleCollection.Add(a_Article);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_ArticleCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public virtual a_ArticleCollection Geta_ArticleListByAll(int mainId, int subId, string fCountry, string fYear, string fkSeason, string permission, string keyword, a_ArticleColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Articles_selListByAll");

                database.AddInParameter(dbCommand, "@MainCatID", DbType.AnsiString, mainId.ToString());
                database.AddInParameter(dbCommand, "@SubCatID", DbType.AnsiString, subId.ToString());
                database.AddInParameter(dbCommand, "@fkCountry", DbType.AnsiString, fCountry);
                database.AddInParameter(dbCommand, "@fYear", DbType.AnsiString, fYear);
                database.AddInParameter(dbCommand, "@fkSeason", DbType.AnsiString, fkSeason);
                database.AddInParameter(dbCommand, "@Permission", DbType.AnsiString, permission);
                database.AddInParameter(dbCommand, "@keyword", DbType.String, keyword);
                database.AddInParameter(dbCommand, "@SortBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@SortOrder", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);

                a_ArticleCollection a_ArticleCollection = new a_ArticleCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Article a_Article = Createa_ArticleFromReader(reader);
                        a_ArticleCollection.Add(a_Article);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_ArticleCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// badpaybad
        /// </summary>
        /// <param name="mainId"></param>
        /// <param name="subId"></param>
        /// <param name="fCountry"></param>
        /// <param name="fYear"></param>
        /// <param name="fkSeason"></param>
        /// <param name="permission"></param>
        /// <param name="keyword"></param>
        /// <param name="orderBy"></param>
        /// <param name="orderDirection"></param>
        /// <param name="dateFrom"></param>
        /// <param name="dateTo"></param>
        /// <param name="publicStatus"></param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecords"></param>
        /// <returns></returns>
        public virtual a_ArticleCollection Geta_ArticleListByCondition(int mainId, int subId, string fCountry, string fYear, string fkSeason, string permission, string keyword, a_ArticleColumns orderBy, OrderDirection orderDirection, 
            string dateFrom, string dateTo, int publicStatus, int featuredType,
            int page, int pageSize, out int totalRecords)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Articles_selListByCondition");

                database.AddInParameter(dbCommand, "@MainCatID", DbType.AnsiString, mainId.ToString());
                database.AddInParameter(dbCommand, "@SubCatID", DbType.AnsiString, subId.ToString());
                database.AddInParameter(dbCommand, "@fkCountry", DbType.AnsiString, fCountry);
                database.AddInParameter(dbCommand, "@fYear", DbType.AnsiString, fYear);
                database.AddInParameter(dbCommand, "@fkSeason", DbType.AnsiString, fkSeason);
                database.AddInParameter(dbCommand, "@Permission", DbType.AnsiString, permission);
                database.AddInParameter(dbCommand, "@PublishStatus", DbType.Int32, publicStatus);
                database.AddInParameter(dbCommand, "@FeaturedType", DbType.Int32, featuredType);
                database.AddInParameter(dbCommand, "@DateFrom", DbType.AnsiString, dateFrom);
                database.AddInParameter(dbCommand, "@DateTo", DbType.AnsiString, dateTo);
                database.AddInParameter(dbCommand, "@keyword", DbType.String, keyword);
                database.AddInParameter(dbCommand, "@SortBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@SortOrder", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);

                a_ArticleCollection a_ArticleCollection = new a_ArticleCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Article a_Article = Createa_ArticleFromReader(reader);
                        a_ArticleCollection.Add(a_Article);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_ArticleCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        //[a_adm_article_sel_v2]
        /// <summary>
        /// Nguyen Phan Du 2009 03 05
        /// </summary>
        /// <param name="CookieID"></param>
        /// <param name="AccessLevel"></param>
        /// <param name="isFeatured"></param>
        /// <param name="Status"></param>
        /// <param name="PageSize"></param>
        /// <param name="PageSetSize"></param>
        /// <param name="sField"></param>
        /// <param name="sValue"></param>
        /// <param name="fDateFrom"></param>
        /// <param name="fDateTo"></param>
        /// <param name="SortBy"></param>
        /// <param name="SortOrder"></param>
        /// <param name="pgNum"></param>
        /// <param name="fCatID"></param>
        /// <param name="fSubCatID"></param>
        /// <returns></returns>
        public virtual DataSet Get_Articles_selListByConditionAmdin(
                string CookieID ,
                int AccessLevel,
                string isFeatured ,
                string Status ,
                int PageSize,
                int PageSetSize,
                string sField ,
                string sValue ,
                string fDateFrom ,
                string fDateTo,
                string SortBy,
                string SortOrder ,
                int pgNum,
                string fCatID,
                string fSubCatID 
            )
        {
            //try
            //{
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_adm_article_sel_v2");

                database.AddInParameter(dbCommand, "@CookieID", DbType.AnsiString, CookieID.ToString());
                database.AddInParameter(dbCommand, "@AccessLevel", DbType.Int32, AccessLevel.ToString());
                database.AddInParameter(dbCommand, "@isFeatured", DbType.AnsiString, isFeatured);
                database.AddInParameter(dbCommand, "@Status", DbType.AnsiString, Status);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, PageSize);
                database.AddInParameter(dbCommand, "@PageSetSize", DbType.Int32, PageSetSize);
                database.AddInParameter(dbCommand, "@sField", DbType.AnsiString, sField);
                database.AddInParameter(dbCommand, "@sValue", DbType.String, sValue);
                database.AddInParameter(dbCommand, "@fDateFrom", DbType.AnsiString, fDateFrom);
                database.AddInParameter(dbCommand, "@fDateTo", DbType.AnsiString, fDateTo);
                database.AddInParameter(dbCommand, "@SortBy", DbType.AnsiString, SortBy);
                database.AddInParameter(dbCommand, "@SortOrder", DbType.AnsiString, SortOrder.ToString());
                database.AddInParameter(dbCommand, "@pgNum", DbType.Int32, pgNum.ToString());
                database.AddInParameter(dbCommand, "@fCatID", DbType.AnsiString, fCatID);
                database.AddInParameter(dbCommand, "@fSubCatID", DbType.AnsiString, fSubCatID);
                database.AddOutParameter(dbCommand, "@sql", DbType.String, 4000);

               
                DataSet reader = database.ExecuteDataSet(dbCommand);


                string sql = database.GetParameterValue(dbCommand, "@sql").ToString();


                return reader;
            //}
            //catch (Exception ex)
            //{
            //    throw ex;
            //}
        }

        //a_articleFeaturedUpload_sel
        /// <summary>
        /// Nguyen Phan Du 2009 03 06
        /// </summary>
        /// <returns></returns>
        public virtual DataSet Get_articleFeaturedUpload()
        {
            Database database = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = database.GetStoredProcCommand("a_articleFeaturedUpload_sel");

            DataSet reader = database.ExecuteDataSet(dbCommand);
            
            return reader;
        }


        //[a_articleUploadDet_up]
        public virtual DataSet a_articleUploadDet_up(
             string Action,
             int AID,
             string Title,
             string CountryID,
             int fYear,
             int SeasonID,
             int WTypeID,
             int SexID,
             string Keywords,
             string ImgOri,
             string ImageW,
             int IID
         )
        {
            //try
            //{
            Database database = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = database.GetStoredProcCommand("a_articleUploadDet_up");

            database.AddInParameter(dbCommand, "@Action", DbType.AnsiString, Action);
            database.AddInParameter(dbCommand, "@AID", DbType.Int32, AID);
            database.AddInParameter(dbCommand, "@Title", DbType.String, Title);
            database.AddInParameter(dbCommand, "@CountryID", DbType.Int32, CountryID);
            database.AddInParameter(dbCommand, "@fYear", DbType.Int32, fYear);
            database.AddInParameter(dbCommand, "@SeasonID", DbType.Int32, SeasonID);
            database.AddInParameter(dbCommand, "@WTypeID", DbType.AnsiString, WTypeID);
            database.AddInParameter(dbCommand, "@SexID", DbType.Int32, SexID);
            database.AddInParameter(dbCommand, "@Keywords", DbType.String, Keywords);
            database.AddInParameter(dbCommand, "@ImgOri", DbType.AnsiString, ImgOri);
            database.AddInParameter(dbCommand, "@ImageW", DbType.AnsiString, ImageW);
            database.AddInParameter(dbCommand, "@IID", DbType.Int32, IID);
            
            //database.AddOutParameter(dbCommand, "@sql", DbType.String, 4000);

            DataSet reader = database.ExecuteDataSet(dbCommand);
            //string sql = database.GetParameterValue(dbCommand, "@sql").ToString();
            return reader;
            //}
            //catch (Exception ex)
            //{
            //    throw ex;
            //}
        }

        //[[a_StatReport_Images]]
        public virtual DataSet a_StatReport_Images(
               string DateTo,
             string DateFrom,
             string SortBy,
             string OrderBy,
             string Top,
             string DocType,
             int pgNum,
             int PageSize,
             int PageSetSize
         )
        {
            //try
            //{
            Database database = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = database.GetStoredProcCommand("a_StatReport_Images");

            database.AddInParameter(dbCommand, "@DateTo", DbType.AnsiString, DateTo);
            database.AddInParameter(dbCommand, "@DateFrom", DbType.AnsiString, DateFrom);
            database.AddInParameter(dbCommand, "@SortBy", DbType.AnsiString, SortBy);
            database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, OrderBy);
            database.AddInParameter(dbCommand, "@Top", DbType.AnsiString, Top);
            database.AddInParameter(dbCommand, "@DocType", DbType.AnsiString, DocType);
            database.AddInParameter(dbCommand, "@pgNum", DbType.Int32, pgNum);
            database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, PageSize);
            database.AddInParameter(dbCommand, "@PageSetSize", DbType.Int32, PageSetSize);
             

            //database.AddOutParameter(dbCommand, "@sql", DbType.String, 4000);

            DataSet reader = database.ExecuteDataSet(dbCommand);
            //string sql = database.GetParameterValue(dbCommand, "@sql").ToString();
            return reader;
            //}
            //catch (Exception ex)
            //{
            //    throw ex;
            //}
        }

        //a_Article_RSS_allsite
        public virtual DataSet a_Article_RSS_allsite()
        {
            //try
            //{
            Database database = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = database.GetStoredProcCommand("a_Article_RSS_allsite");


            DataSet reader = database.ExecuteDataSet(dbCommand);
            //string sql = database.GetParameterValue(dbCommand, "@sql").ToString();
            return reader;
            //}
            //catch (Exception ex)
            //{
            //    throw ex;
            //}
        }

        //a_Article_RSS
        public virtual DataSet a_Article_RSS(
               int TypeCat,
             string CatID,
             string NumTop
         )
        {
            //try
            //{
            Database database = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = database.GetStoredProcCommand("a_Article_RSS");

            database.AddInParameter(dbCommand, "@TypeCat", DbType.AnsiString, TypeCat);
            database.AddInParameter(dbCommand, "@CatID", DbType.AnsiString, CatID);
            database.AddInParameter(dbCommand, "@NumTop", DbType.AnsiString, NumTop);
            
            //database.AddOutParameter(dbCommand, "@sql", DbType.String, 4000);

            DataSet reader = database.ExecuteDataSet(dbCommand);
            //string sql = database.GetParameterValue(dbCommand, "@sql").ToString();
            return reader;
            //}
            //catch (Exception ex)
            //{
            //    throw ex;
            //}
        }

        /// <summary>
        /// a_StatReport_Articles
        /// </summary>
        /// <returns></returns>
        /// 
        public virtual DataSet a_StatReport_Articles(
              string DateTo,
            string DateFrom,
            string SortBy,
            string OrderBy,
            string Top,
            string DocType,
            int pgNum,
            int PageSize,
            int PageSetSize
        )
        {
            //try
            //{
            Database database = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = database.GetStoredProcCommand("a_StatReport_Articles");

            database.AddInParameter(dbCommand, "@DateTo", DbType.AnsiString, DateTo);
            database.AddInParameter(dbCommand, "@DateFrom", DbType.AnsiString, DateFrom);
            database.AddInParameter(dbCommand, "@SortBy", DbType.AnsiString, SortBy);
            database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, OrderBy);
            database.AddInParameter(dbCommand, "@Top", DbType.AnsiString, Top);
            database.AddInParameter(dbCommand, "@DocType", DbType.AnsiString, DocType);
            database.AddInParameter(dbCommand, "@pgNum", DbType.Int32, pgNum);
            database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, PageSize);
            database.AddInParameter(dbCommand, "@PageSetSize", DbType.Int32, PageSetSize);

            //database.AddOutParameter(dbCommand, "@sql", DbType.String, 4000);

            DataSet reader = database.ExecuteDataSet(dbCommand);
            //string sql = database.GetParameterValue(dbCommand, "@sql").ToString();
            return reader;
            //}
            //catch (Exception ex)
            //{
            //    throw ex;
            //}
        }

        /// <summary>
        /// a_adm_stats_sel
        /// </summary>
        /// <returns></returns>
        /// 
        public virtual DataSet a_adm_stats_sel(
              string sField,
            string sValue,
            string SortBy,
            string OrderBy,
            int pgNum,
            int PageSize,
            int PageSetSize
        )
        {
            //try
            //{
            Database database = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = database.GetStoredProcCommand("a_adm_stats_sel");

            database.AddInParameter(dbCommand, "@sField", DbType.String, sField);
            database.AddInParameter(dbCommand, "@sValue", DbType.String, sValue);
            database.AddInParameter(dbCommand, "@SortBy", DbType.String, SortBy);
            database.AddInParameter(dbCommand, "@SortOrder", DbType.String, OrderBy);           
            database.AddInParameter(dbCommand, "@pgNum", DbType.Int32, pgNum);
            database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, PageSize);
            database.AddInParameter(dbCommand, "@PageSetSize", DbType.Int32, PageSetSize);

            //database.AddOutParameter(dbCommand, "@sql", DbType.String, 4000);

            DataSet reader = database.ExecuteDataSet(dbCommand);
            //string sql = database.GetParameterValue(dbCommand, "@sql").ToString();
            return reader;
            //}
            //catch (Exception ex)
            //{
            //    throw ex;
            //}
        }

        //a_adm_stats_det_sel
        public virtual DataSet a_adm_stats_det_sel(
          string RefID ,
int PageSize ,
int PageSetSize ,
int typeDate , 				//-- 0: filter rangeDate			1: filter by specify date form and date to
string rangeDate , 
string fDateFrom ,
string fDateTo ,
string SortBy ,
string SortOrder ,
int pgNum 
        )
        {
            //try
            //{
            Database database = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = database.GetStoredProcCommand("a_adm_stats_det_sel");

            database.AddInParameter(dbCommand, "@RefID", DbType.String, RefID);
            database.AddInParameter(dbCommand, "@typeDate", DbType.Int32, typeDate);
            database.AddInParameter(dbCommand, "@rangeDate", DbType.String, rangeDate);
            database.AddInParameter(dbCommand, "@fDateFrom", DbType.String, fDateFrom);
            database.AddInParameter(dbCommand, "@fDateTo", DbType.String, fDateTo);
            database.AddInParameter(dbCommand, "@SortBy", DbType.String, SortBy);
            database.AddInParameter(dbCommand, "@SortOrder", DbType.String, SortOrder);
            database.AddInParameter(dbCommand, "@pgNum", DbType.Int32, pgNum);
            database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, PageSize);
            database.AddInParameter(dbCommand, "@PageSetSize", DbType.Int32, PageSetSize);

            //database.AddOutParameter(dbCommand, "@sql", DbType.String, 4000);

            DataSet reader = database.ExecuteDataSet(dbCommand);
            //string sql = database.GetParameterValue(dbCommand, "@sql").ToString();
            return reader;
            //}
            //catch (Exception ex)
            //{
            //    throw ex;
            //}
        }

        public virtual a_ArticleCollection GetFeaturedArticles()
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_index_GetFeaturedArticles");
               
                a_ArticleCollection a_ArticleCollection = new a_ArticleCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Article a_Article = Createa_ArticleFromReader(reader);
                        a_ArticleCollection.Add(a_Article);
                    }
                    reader.Close();
                }
                return a_ArticleCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
      

        public virtual a_ArticleCollection GetLastestArticles()
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Articles_selListByLastest");

                a_ArticleCollection a_ArticleCollection = new a_ArticleCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Article a_Article = Createa_ArticleFromReader(reader);
                        a_ArticleCollection.Add(a_Article);
                    }
                    reader.Close();
                }
                return a_ArticleCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public virtual a_ArticleCollection GetRecruitmentArticles()
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Articles_GetListRecruitment");

                a_ArticleCollection a_ArticleCollection = new a_ArticleCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Article a_Article = Createa_ArticleFromReader(reader);
                        a_ArticleCollection.Add(a_Article);
                    }
                    reader.Close();
                }
                return a_ArticleCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #region a_Articles_selListFavBySSID methods
        public virtual a_ArticleCollection a_Articles_selListFavBySSID(string ssid, a_ArticleColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Articles_selListFavBySSID");

                database.AddInParameter(dbCommand, "@SSID", DbType.String, ssid);
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);

                a_ArticleCollection a_ArticleCollection = new a_ArticleCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Article a_Article = Createa_ArticleFromReader(reader);
                        a_ArticleCollection.Add(a_Article);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_ArticleCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public virtual a_ArticleCollection a_Articles_selListFavBySSID(string ssid, a_ArticleColumns orderBy, OrderDirection orderDirection)
        {
            int totalRecords = 0;
            return a_Articles_selListFavBySSID(ssid, orderBy, orderDirection, 0, 0, out totalRecords);
        }

        #endregion
    }
}